package com.softer.wxzj.controller;

import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.aop.annotation.LogRecord;
import com.softer.wxzj.common.aop.enums.LogOperationType;
import com.softer.wxzj.common.aop.enums.LogOperationUnit;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.util.ShiroUserUtil;
import com.softer.wxzj.entity.*;
import com.softer.wxzj.service.BaseHouseInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

/**
 * @description:房屋信息
 * @ClassName: BaseHouseInfoController
 * @author: zcz
 * @date: 2019-05-20 16:31
 */
@RestController
@RequestMapping("/baseHouseInfo")
@Slf4j
public class BaseHouseInfoController {
    @Autowired
    BaseHouseInfoService baseHouseInfoService;

    @RequestMapping("/getRoomNameCountById")
    @LogRecord(description = "查询房间名是否重复", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.INSERT)
    public int getRoomNameCountById(String id, String roomName) {
        return baseHouseInfoService.getRoomNameCountById(id, roomName);
    }

    @RequestMapping("/add")
    @LogRecord(description = "房屋信息新增", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.INSERT)
    public CommonResult add(BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.insert(baseHouseInfo);
    }

    @RequestMapping("/addHouseByCell")
    @LogRecord(description = "房屋信息新增", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.INSERT)
    public CommonResult addHouseByCell(BaseHouseInfo baseHouseInfo, @RequestParam("stepType") String stepType) {
        return baseHouseInfoService.insertHouseByCell(baseHouseInfo, stepType);
    }

    @RequestMapping("/update")
    @LogRecord(description = "房屋信息修改", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult update(BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.update(baseHouseInfo);
    }

    @RequestMapping("/delete")
    @LogRecord(description = "房屋信息逻辑删除", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.DELETE)
    public CommonResult delete(String id) {
        return baseHouseInfoService.delete(id);
    }

    @RequestMapping("/getInfo")
    @LogRecord(description = "根据id获取房屋信息详情", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public BaseHouseInfo getInfo(String id) {
        return baseHouseInfoService.getInfo(id);
    }

    @RequestMapping("/getList")
    @LogRecord(description = "房屋信息列表", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseHouseInfo> getList(Page page, BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.getList(page, baseHouseInfo);
    }

    @RequestMapping("/getAllList")
    @LogRecord(description = "房屋信息列表,不分页", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public List<BaseHouseInfo> getList(BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.getAllList(baseHouseInfo);
    }

    @RequestMapping("/getHouseAndOwnerList")
    @LogRecord(description = "房屋信息维护界面列表", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseHouseInfo> getHouseAndOwnerList(Page page, BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.getHouseAndOwnerList(page, baseHouseInfo);
    }

    /**
     * 房屋信息变更
     *
     * @param houseChange
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateToSave")
    @LogRecord(description = "房屋信息变更", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult updateToSave(@RequestBody HouseChange houseChange) throws Exception {
        return baseHouseInfoService.updateToSave(houseChange.getBaseHouseInfo(), houseChange.getModifyType(), houseChange.getModifyCause());
    }

    /**
     * 获取房屋调换页面列表
     * 调换只针对已经交存过的房屋，(交存过的记账成功的 ，没有在途业务的（交存，退款，使用）)
     *
     * @param page          分页信息
     * @param baseHouseInfo 查询实体
     * @return 分页实体
     */
    @RequestMapping("/getChangeHouseList")
    @LogRecord(description = "房屋调换界面列表", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseHouseInfo> getChangeHouseList(Page page, BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.getChangeHouseList(page, baseHouseInfo);
    }

    /**
     * 获取第二层房屋调换列表
     * 根据传入的房间ID，查询当前房间所在小区的所有房间
     *
     * @param page          分页信息
     * @param baseHouseInfo 查询实体
     * @return 分页实体
     */
    @RequestMapping("/getSecondChangeHouseList")
    @LogRecord(description = "第二层房屋调换列表", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseHouseInfo> getSecondChangeHouseList(Page page, BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.getSecondChangeHouseList(page, baseHouseInfo);
    }

    /**
     * 房屋调换
     *
     * @param beforeRoomId 更换前房间Id
     * @param afterRoomId  更换后房屋ID
     * @return 调换结果
     */
    @RequestMapping("/change")
    @LogRecord(description = "调换", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult change(String beforeRoomId, String afterRoomId) {
        return baseHouseInfoService.change(beforeRoomId, afterRoomId);
    }

    /**
     * 新增或修改批量操作
     *
     * @param baseHouseInfoList
     * @return
     */
    @RequestMapping("/insertOrUpdate")
    @LogRecord(description = "房屋新增或修改", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.INSERT_OR_UPDATE)
    public CommonResult insertOrUpdate(@RequestBody List<BaseHouseInfo> baseHouseInfoList) {
        return baseHouseInfoService.insertOrUpdate(baseHouseInfoList);
    }

    /**
     * 新增或修改批量操作
     *
     * @param baseHouseInfoList
     * @return
     */
    @RequestMapping("/insertOrUpdateToView")
    @LogRecord(description = "房屋新增或修改", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.INSERT_OR_UPDATE)
    public CommonResult insertOrUpdateToView(@RequestBody List<BaseHouseInfo> baseHouseInfoList) {
        //过滤已发生过缴存的
        //过滤已缴存或处于缴存中的房屋
        Iterator<BaseHouseInfo> itr = baseHouseInfoList.iterator();
        while (itr.hasNext()) {
            if (NumStrEnum.ONE.getNum().equals(itr.next().getIsDueState())) {
                itr.remove();
            }
        }
        return baseHouseInfoService.insertOrUpdate(baseHouseInfoList);
    }

    /**
     * 房屋调换审核
     *
     * @param id      变化ID
     * @param opinion 审批意见
     * @param mark    审批通过 audit_pass
     *                审批否决 audit_veto
     *                审批退回 audit_back
     * @return 操作结果
     */
    @RequestMapping("/changeModifyAudit")
    @LogRecord(description = "房屋调换审核", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult changeModifyAudit(@RequestParam("id") String id, String opinion, @RequestParam("mark") String mark) {
        return baseHouseInfoService.changeModifyAudit(id, opinion, mark);
    }

    /**
     * 查询房屋调换审核列表
     *
     * @param page                  分页信息
     * @param baseBuildModifyDetail 查询实体
     *                              oldAddress 房屋地址
     *                              createTime 申请开始时间
     *                              updateTime 申请结束时间
     *                              creater 申请人
     * @return 查询结果
     */
    @RequestMapping("/getChangeModifyList")
    @LogRecord(description = "查询房屋调换审核列表", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseBuildModifyDetail> getChangeModifyList(Page page, BaseBuildModifyDetail baseBuildModifyDetail) {
        return baseHouseInfoService.getChangeModifyList(page, baseBuildModifyDetail);
    }

    /**
     * 查询房屋调换详情
     *
     * @param id 变更ID
     * @return 详情信息
     */
    @RequestMapping("/getChangeDetail")
    @LogRecord(description = "查询房屋调换详情", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public BaseBuildModifyDetail getChangeDetail(String id) {
        return baseHouseInfoService.getChangeDetail(id);
    }

    /**
     * 查询房屋调换查询列表
     *
     * @param page                  分页信息
     * @param baseBuildModifyDetail 查询实体
     *                              oldAddress 调换前房屋地址
     *                              newAddress 调换后房屋地址
     *                              createTime 申请开始时间
     *                              updateTime 申请结束时间
     *                              modifyCause 变更原因
     *                              creater 变更人
     * @return 查询结果
     */
    @RequestMapping("/getChangeModifyInfoList")
    @LogRecord(description = "查询房屋调换查询列表", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseBuildModifyDetail> getChangeModifyInfoList(Page page, BaseBuildModifyDetail baseBuildModifyDetail) {
        return baseHouseInfoService.getChangeModifyInfoList(page, baseBuildModifyDetail);
    }

    /**
     * 模糊查询房屋调换变更列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 房屋调换变更列表
     */
    @RequestMapping("/fuzzySearch")
    @LogRecord(description = "模糊查询房屋调换变更列表", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseHouseInfo> fuzzySearch(Page page, String param) {
        return baseHouseInfoService.fuzzySearch(page, param);
    }

    @RequestMapping("/getListToView")
    @LogRecord(description = "房屋信息列表,用于视图界面 ", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public List<BaseHouseInfo> getListToView(BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.getListToView(baseHouseInfo);
    }

    @RequestMapping("/getAllListToView")
    @LogRecord(description = "房屋信息列表,用于视图界面 ", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public List<BaseHouseInfo> getAllListToView(BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.getAllListToView(baseHouseInfo);
    }

    @RequestMapping("/getPageListToView")
    @LogRecord(description = "房屋信息列表,用于视图界面,带分页 ", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseHouseInfo> getPageListToView(Page page, BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.getPageListToView(page, baseHouseInfo);
    }

    /**
     * 房屋批量修改
     *
     * @param baseHouseInfoList
     * @return
     */
    @RequestMapping("/updateBatch")
    @LogRecord(description = "房屋批量修改", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult updateBatch(@RequestBody List<BaseHouseInfo> baseHouseInfoList) {
        return baseHouseInfoService.updateBatch(baseHouseInfoList);
    }

    @RequestMapping("/getBindSubHouseList")
    @LogRecord(description = "查询关联附房列表 ", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseHouseInfo> getBindSubHouseList(Page page, BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.getBindSubHouseList(page, baseHouseInfo);
    }

    @RequestMapping("/getOwnerListToView")
    @LogRecord(description = "查询业主批量为护列表 ", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseHouseInfo> getOwnerListToView(Page page, BaseHouseInfo baseHouseInfo, String arcType) {
        return baseHouseInfoService.getOwnerListToView(page, baseHouseInfo, arcType);
    }

    /**
     * 根据自然幢id查询单元名列表  填充下拉框
     *
     * @param buildNo
     * @return
     */
    @RequestMapping("/getCellNameListByBuildNo")
    public List<String> getCellNameListByBuildNo(String buildNo) {
        return baseHouseInfoService.getCellNameListByBuildNo(buildNo);
    }

    /**
     * 根据自然幢id查询房屋性质列表  填充下拉框
     *
     * @param buildNo
     * @return
     */
    @RequestMapping("/getHouseUseListByBuildNo")
    public List<String> getHouseUseListByBuildNo(String buildNo) {
        return baseHouseInfoService.getHouseUseListByBuildNo(buildNo);
    }

    @RequestMapping("/deleteBatchToView")
    @LogRecord(description = "批量删除房间信息", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.DELETE)
    public CommonResult deleteBatchToView(@RequestParam("idList") String idList) {
        log.info("删除房间信息:{}", idList, ShiroUserUtil.getUserName());
        return baseHouseInfoService.deleteBatchToView(idList);
    }

    @RequestMapping("/insertByFloor")
    @LogRecord(description = "房屋批量维护添加层", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.DELETE)
    public CommonResult insertByFloor(@RequestParam("cellNo") String cellNo, @RequestParam("houseNum") String houseNum, @RequestParam("floorNo") String floorNo) {
        log.info("删除房间信息:{}", cellNo, houseNum, floorNo, ShiroUserUtil.getUserName());
        return baseHouseInfoService.insertByFloor(cellNo, houseNum, floorNo);
    }

    @RequestMapping("/getCellByBuildNoAndFloorNoToInput")
    @LogRecord(description = "根据自然幢编号和楼层号查询可添加的单元列表  用于填充添加层界面下拉框 ", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public List<BaseHouseInfo> getCellByBuildNoAndFloorNoToInput(@RequestParam("floorNo") String floorNo, @RequestParam("buildNo") String buildNo) {
        return baseHouseInfoService.getCellByBuildNoAndFloorNoToInput(floorNo, buildNo);
    }

    @RequestMapping("/getHouseListToRef")
    @LogRecord(description = "资金退款-部分退款列表 ", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseHouseInfo> getHouseListToRef(Page page, BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.getHouseListToRef(page, baseHouseInfo);
    }

    /**
     * 业主过户和销户时的房屋信息
     *
     * @param houseId
     * @return
     */
    @RequestMapping("/getHouseInfo")
    @LogRecord(description = "业主过户的房屋信息 ", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public BaseHouseInfo getHouseInfo(String houseId) {
        return baseHouseInfoService.getHouseInfo(houseId);

    }

    @RequestMapping("/getListToRef")
    @LogRecord(description = "查询批量退款业主列表", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseHouseInfo> getListToRef(Page page, @RequestParam("buildNo") String buildNo, @RequestParam("refType") String refType,String arcType) {
        log.info("查询批量退款业主列表,操作人:{}", ShiroUserUtil.getUserName());
        return baseHouseInfoService.getListToRef(page, buildNo, refType);
    }

    /**
     * 根据小区ID查询房间列表
     *
     * @param regionId 小区ID
     * @param buildId  自然幢ID
     * @param cellId   单元ID
     * @return 房间列表
     */
    @PostMapping("/getHouseListByRegionId")
    public List<BaseHouseInfo> getHouseListByRegionId(String regionId, String buildId, String cellId) {
        return baseHouseInfoService.getHouseListByRegionId(regionId, buildId, cellId);
    }

    /**
     * 房屋变更审核
     *
     * @param id
     * @param opinion
     * @param mark
     * @return
     */
    @PostMapping("/changeResidentialCommittee")
    @LogRecord(description = "房屋变更审核", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult changeResidentialCommittee(String id, String opinion, String mark) {
        return baseHouseInfoService.changeResidentialCommittee(id, opinion, mark);
    }

    /**
     * 根据id查询变更详情
     *
     * @param id 变更类型id
     * @return
     */
    @RequestMapping("/getModifyDetailList")
    @LogRecord(description = "根据id查询变更详情", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    public PageInfo<BaseModifyDetail> getModifyDetailList(Page page, String id) {
        return baseHouseInfoService.getModifyDetailList(page, id);
    }

    /**
     * 根据id查询变更详情
     *
     * @param baseHouseInfo
     * @return
     */
    @RequestMapping("/getListNotDep")
    @LogRecord(description = "变更", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    PageInfo<BaseHouseInfo> getListNotDep(Page page, BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.getListNotDep(page, baseHouseInfo);
    }

    /**
     * 房屋结构变更
     */
    @RequestMapping("/updateToSaveByHouse")
    @LogRecord(description = "房屋结构变更", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.SELECT)
    CommonResult updateToSaveByHouse(@RequestBody List<BaseHouseInfo> baseHouseInfo) throws Exception {
        return baseHouseInfoService.updateToSaveByHouse(baseHouseInfo);
    }

    /**
     * 房屋结构变更审核
     *
     * @param id      变更Id
     * @param opinion 审核意见
     * @param mark    审批通过 audit_pass
     *                审批否决 audit_veto
     *                审批退回 audit_back
     * @return 审核结果
     */
    @RequestMapping("/modifyAuditHouse")
    @LogRecord(description = "房屋结构变更审核", operationUnit = LogOperationUnit.BASE_REGION_INFO, operationType = LogOperationType.UPDATE)
    public CommonResult modifyAuditHouse(String id, String buildId, String opinion, String mark) {
        return baseHouseInfoService.modifyAuditHouse(id, buildId, opinion, mark);
    }

    /**
     * 房屋结构变更--房屋信息
     *
     * @param reqNo  变更Id
     * @param cellId 审核意见
     * @return 审核结果
     */
    @RequestMapping("/selectAllHouseAndChange")
    @LogRecord(description = "房屋结构变更--房屋信息", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.SELECT)
    public List<BaseHouseInfo> selectAllHouseAndChange(String reqNo, String cellId) {
        return baseHouseInfoService.selectAllHouseAndChange(reqNo, cellId);
    }

    /**
     * 删除房屋和业主
     *
     * @param id
     * @return
     */
    @RequestMapping("/delHouseAndOwner")
    @LogRecord(description = "删除房屋和业主", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.DELETE)
    public CommonResult delHouseAndOwner(String id) {
        return baseHouseInfoService.delHouseAndOwner(id);
    }

    /**
     * 查询房屋和业主
     *
     * @param id
     * @return
     */
    @RequestMapping("/houseAndOwner")
    @LogRecord(description = "查询房屋和业主", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.DELETE)
    public PageInfo<BaseHouseInfo> houseAndOwner(Page page, BaseHouseInfo baseHouseInfo) {
        return baseHouseInfoService.houseAndOwner(page, baseHouseInfo);
    }

    /**
     * 查询房屋和业主
     *
     * @param id
     * @return
     */
    @RequestMapping("/selectHouseById")
    @LogRecord(description = "查询房屋和业主", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.DELETE)
    BaseHouseInfo selectHouseById(String id) {
        return baseHouseInfoService.selectHouseById(id);
    }

    @RequestMapping("/selectCountByBuildId")
    @LogRecord(description = "自然幢合计", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.DELETE)
    BaseHouseInfo selectCountByBuildId(String buildNo) {
        return baseHouseInfoService.selectCountByBuildId(buildNo);
    }

    @RequestMapping("/selectCountByRegionNo")
    @LogRecord(description = "小区合计", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.DELETE)
    BaseHouseInfo selectCountByRegionNo(String regionNo) {
        return baseHouseInfoService.selectCountByRegionNo(regionNo);
    }

    /**
     * 资金补齐审核详情
     * @param id deq_rq id
     * @return
     */
    @RequestMapping("/selectBydepReqList")
    @LogRecord(description = "资金补齐审核详情", operationUnit = LogOperationUnit.BASE_HOUSE_INFO, operationType = LogOperationType.DELETE)
    PageInfo<BaseHouseInfo> selectBydepReqList(Page page,String id) {
        return baseHouseInfoService.selectBydepReqList(page,id);
    }
}
